package com.hifar.test.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.fw.utils.lang.NumberUtils;
import com.hifar.fw.utils.lang.StringUtils;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.sys.*;
import com.hifar.test.entrust.constant.TestExceptionConsts;
import com.hifar.test.test.pojo.HfEnvTestException;
import com.hifar.test.test.pojo.VO.HfEnvTestExceptionSaveVO;
import com.hifar.test.test.service.IHfEnvTestExceptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhuWeiWei
 * @description 试验异常
 * @date 2024/12/24 13:55
 */
@RestController
@RequestMapping("/api/busi/test/testException")
@Api("试验异常")
public class HfEnvTestExceptionController {
    @Autowired
    private IHfEnvTestExceptionService testExceptionService;

    @PostMapping("/add")
    @ApiModelProperty("新增试验异常")
    public BusiResponse add(@RequestBody @Validated HfEnvTestExceptionSaveVO testException) {
        testExceptionService.saveException(testException);
        return BusiResponse.OK();
    }

    @PostMapping("/edit")
    @ApiModelProperty("编辑试验异常")
    public BusiResponse edit(@RequestBody @Validated HfEnvTestExceptionSaveVO testException) {
        testExceptionService.editException(testException);
        return BusiResponse.OK();
    }


    @PostMapping("/listPage")
    @ApiOperation(value = "试验异常功能:异常列表查询")
    public BusiResponse listPage(@RequestBody Map<String, Object> queryParam) {
        int pageNo = NumberUtils.parseInt(queryParam.get("pageNo"), 0);
        int pageSize = NumberUtils.parseInt(queryParam.get("pageSize"), 20);
        String testId = StringUtils.trimNull(queryParam.get("testId"));
        Assert.hasText(testId, "testId不能为空!");
        QueryWrapper<HfEnvTestException> queryWrapper = RequestConver.getPostParam(queryParam);
        queryWrapper.in("secret_level", SystemLoginUser.dataSecretSet);
        queryWrapper.eq("test_id", testId);
        queryWrapper.orderByDesc("create_time");
        Page<HfEnvTestException> page = new Page<>(pageNo, pageSize);
        Page<HfEnvTestException> pageData = testExceptionService.page(page, queryWrapper);
        /*计算分页信息返回给前端*/
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "试验异常:逻辑删除")
    public BusiResponse delete(@RequestBody List<String> idList) {
        QueryWrapper<HfEnvTestException> delete = new QueryWrapper<>();
        delete.in("id", idList);
        testExceptionService.remove(delete);
        return BusiResponse.ok("删除成功！");
    }


    @GetMapping("/queryById")
    @ApiOperation(value = "试验异常:查询详情")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfEnvTestException hfEnvTestException = testExceptionService.getById(id);
        Assert.notNull(hfEnvTestException, "数据不存在!");
        List<BaseOssFiles> filesList = MinioUtil.listByRefId(id, TestExceptionConsts.TEST_EXCEPTION_REF_TYPE_FILE,true);
        HfEnvTestExceptionSaveVO hfEnvTestExceptionSaveVO = new HfEnvTestExceptionSaveVO();
        BeanUtils.copyProperties(hfEnvTestException, hfEnvTestExceptionSaveVO);
        hfEnvTestExceptionSaveVO.setFiles(filesList);
        return BusiResponse.OK(hfEnvTestExceptionSaveVO);
    }


}
